//
//  UIImage+UIColor.swift
//  LGMusic
//
//  Created by 梁尚嘉 on 2017/3/24.
//  Copyright © 2017年 LimeGah. All rights reserved.
//

import UIKit

extension UIImage {


    /// 由颜色初始化一个对应颜色的图片
    ///
    /// - Parameter color: 颜色
    /// - Returns: 图片
    class func image(with color:UIColor) -> UIImage {
        let rect = CGRect.init(x: 0, y: 0, width: 1, height: 1)
        UIGraphicsBeginImageContext(rect.size)
        let context = UIGraphicsGetCurrentContext()

        context?.setFillColor(color.cgColor)
        context?.fill(rect)

        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return image!
    }



    /// c通过颜色改变图片为填充当前颜色的图片
    ///
    /// - Parameter color: 颜色
    /// - Returns: 图片
    func image(with color:UIColor) -> UIImage {

        UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
        let context = UIGraphicsGetCurrentContext()
        context?.translateBy(x: 1.0, y: -1.0)
        context?.setBlendMode(CGBlendMode.normal)
        let rect = CGRect.init(x: 0, y: 0, width: self.size.width, height: self.size.height)
        context?.clip(to: rect, mask: self.cgImage!)
        color.setFill()
        context?.fill(rect)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image!
    }

}
